package com.sakura.probe.nosql.redis;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sakura.probe.common.util.JwtTokenUtil;
import com.sakura.probe.pojo.PersonInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key,value);
    }


    @Override
    public void set(String key, String value, long expire) {
        redisTemplate.opsForValue().set(key,value,expire, TimeUnit.SECONDS);
    }

    @Override
    public void setList(String key, String value) {
        LinkedList<String> list;
        if (redisTemplate.hasKey(key)){
            list = (LinkedList<String>) redisTemplate.opsForList().leftPop(key);
        }else {
            list = new LinkedList<>();
        }
        list.add(value);
        redisTemplate.opsForList().leftPush(key,list);

    }

    @Override
    public void setSet(String key, String value) {

        HashSet<String> set;
        if (redisTemplate.hasKey(key)){
            set = (HashSet<String>) redisTemplate.opsForSet().pop(key);
        }else {
            set = new HashSet<>();
        }
        set.add(value);
        redisTemplate.opsForSet().add(key,set);
    }

    @Override
    public void setSet(String key, Set<String> set) {
        redisTemplate.opsForSet().add(key,set);
    }

    @Override
    public Set<String> keys(String key) {

        return redisTemplate.keys(key+":*");
    }

    @Override
    public String get(Object key) {
        return String.valueOf(redisTemplate.opsForValue().get(key));
    }

    @Override
    public List<String> getList(String key) {
        return (List<String>) redisTemplate.opsForList().leftPop(key);
    }


    @Override
    public Set<String> getSet(String key) {
        return (Set<String>) redisTemplate.opsForSet().pop(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public void remove(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return redisTemplate.opsForValue().increment(key,delta);
    }

    @Override
    public Long decrement(String key, long delta) {
        return redisTemplate.opsForValue().decrement(key,delta);
    }

    @Override
    public boolean exist(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public PersonInfo getPersonByToken(String token) {
        String userId = JwtTokenUtil.getUsernameFromToken(token);
        if (userId!=null&&redisTemplate.hasKey(RedisKey.getPersonInfo()+userId)){
            String personStr = String.valueOf(redisTemplate.opsForValue().get(RedisKey.getPersonInfo()+userId));
            ObjectMapper mapper = new ObjectMapper();
            PersonInfo person = null;
            try {
                person = mapper.readValue(personStr,PersonInfo.class);
            } catch (JsonProcessingException e) {
                log.info("转换成实体类失败:{}",e.getMessage());
            }
            return person;
        }else {
            return null;
        }

    }


}
